<template>
  <div class="app-container">
    <div class="body">
      <el-row>
        <el-col :span="24">
          <!-- 查询表单 -->
          <el-row>
            <el-form
              :inline="true"
              size="mini"
              :model="searchForm"
              ref="searchForm"
              class="demo-form-inline"
            >
              <el-form-item label="关键字">
                <el-input
                  clearable
                  v-model="searchForm.keyword"
                  placeholder="请输入配置项"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="success"
                  icon="el-icon-plus"
                  @click="handleSave"
                  >新增</el-button
                >
              </el-form-item>
              <el-form-item>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  @click="handleBatchDeleted()"
                  >批量刪除</el-button
                >
              </el-form-item>
            </el-form>
          </el-row>
          <!-- 表格 -->
          <el-row>
            <el-table
              ref="multipleTable"
              :data="tableData"
              tooltip-effect="dark"
              style="width: 100%"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column prop="id" label="编号"> </el-table-column>
              <el-table-column prop="type" label="类型"> </el-table-column>
              <el-table-column prop="prop" label="配置项"> </el-table-column>
              <el-table-column prop="value" label="配置属性"> </el-table-column>
              <el-table-column prop="des" label="配置描述"> </el-table-column>

              <el-table-column width="180" fixed="right" label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    class="el-icon-edit"
                    @click="handleEdit(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="mini"
                    type="danger"
                    class="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-row>
          <!-- 分页 -->
          <el-row>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="searchForm.currentPage"
              :page-sizes="[3, 5, 10, 20]"
              :page-size="searchForm.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
            >
            </el-pagination>
          </el-row>
        </el-col>
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogFormVisible"
          width="40%"
        >
          <el-form
            :model="editForm"
            status-icon
            ref="editForm"
            :rules="editFormRules"
            label-width="80px"
          >
            <el-form-item label="类型" prop="type">
              <el-input v-model="editForm.type" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="配置项" prop="prop">
              <el-input v-model="editForm.prop" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="配置属性" prop="value">
              <el-input v-model="editForm.value" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="配置描述" prop="des">
              <el-input
                type="textarea"
                :rows="2"
                v-model="editForm.des"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleEditForm('editForm')"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  listData,
  saveOrUpdate,
  deleteById,
  batchDelete,
} from "@/api/system/configInfo";

export default {
  name: "User",
  data() {
    return {
      dialogTitle: "",
      dialogFormVisible: false,
      total: 0,
      tableData: [],
      editForm: {},
      searchForm: {
        keyword: "",
        currentPage: 1,
        pageSize: 3,
      },
      deletedIds: [],
      editFormRules: {
        type: [
          {
            required: true,
            message: "系统配置属性类型不能为空！",
            trigger: "blur",
          },
        ],
        prop: [
          {
            required: true,
            message: "系统配置属性名不能为空！",
            trigger: "blur",
          },
        ],
        value: [
          {
            required: true,
            message: "系统配置属性值不能为空！",
            trigger: "blur",
          },
        ],
        des: [
          {
            required: true,
            message: "系统配置属性描述内容不能为空！",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleCurrentChange(currentPage) {
      this.searchForm.currentPage = currentPage;
      // 当每页显示数发生变动时触发
      this.fetchConfigData(this.searchForm);
    },
    handleSizeChange(size) {
      this.searchForm.pageSize = size;
      // 当前页发生变动时触发
      this.fetchConfigData(this.searchForm);
    },

    fetchConfigData(params) {
      listData(params).then((res) => {
        this.total = res.data.totalCount;
        this.tableData = res.data.data;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      const temp = [];
      val.forEach((logger) => {
        temp.push(logger.id);
      });
      // 去除重复的 id
      this.deletedIds = Array.from(new Set(temp));
    },
    handleBatchDeleted() {
      batchDelete(this.deletedIds).then((res) => {
        this.deletedIds = [];
        this.fetchConfigData(this.searchForm);
        this.$message.success("批量删除成功！");
      });
    },
    onSubmit() {
      this.fetchConfigData(this.searchForm);
      this.searchForm.currentPage = 1;
    },
    handleEdit(data) {
      this.dialogFormVisible = true;
      this.dialogTitle = "编辑用户";
      this.editForm = data;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除这一条记录, 确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          deleteById(row.id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.fetchConfigData(this.searchForm);
          });
        })
        .catch((err) => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEditForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveOrUpdate(this.editForm).then((res) => {
            this.$message.success("操作成功！");
            this.dialogFormVisible = false;
            this.fetchConfigData(this.searchForm);
          });
        } else {
          return false;
        }
      });
    },
    handleSave() {
      this.dialogTitle = "新增系统配置";
      this.resetForm("editForm");
      this.dialogFormVisible = true;
      this.editForm = {};
    },
  },
  created() {
    this.fetchConfigData();
  },
};
</script>

<style lang="css" scoped>
.line {
  text-align: center;
}

.el-pagination {
  text-align: right;
}
</style>
